var express = require('express');
var router = express.Router();
const usersService = require('../src/services/usersServices');
const multiparty = require("multiparty");
const path = require('path');

/**
 *@swagger
 * /users:
 *    post:
 *      tags:
 *      - users
 *      summary: 增加用户
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:          
 *                  type: string
 *                  description: 用户昵称
 *                username:    
 *                  type: integer
 *                  description: 用户姓名
 *                pwd:          
 *                  type: string
 *                  description: 用户密码
 *                gender:
 *                  type: string
 *                  description: 性别
 *                birth:
 *                  type: string
 *                  description: 用户生日
 *                phone:
 *                  type: integer
 *                  description: 用户电话
 *                integral:
 *                  type: integer
 *                  description: 会员积分
 *                coupons:
 *                  type: string
 *                  description: 优惠券
 *                address:
 *                  type: string
 *                  description: 地址
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
**/ 
router.post('/',async function(req,res){
    let {name,username,pwd,gender,birth,phone,integral,coupons,address,headImg} = req.body;
    console.log(req.body);
     let data = await usersService.insert({name,username,pwd,gender,birth,phone,integral,coupons,address,headImg});
     res.send(data);
});

router.get('/',async function(req,res){
    let {type,value,current,pageSize} = req.query;
    let searchParams = {};
    if(type){
        searchParams[type] = value;
    }
    let data = await usersService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
    res.send(data);
});

router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await usersService.findById(id);
    res.send(data);
});

router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await usersService.delete(id);
    res.send(data);
});

router.put('/:id',async function(req,res){
    let {id} = req.params;
    let {name,username,pwd,gender,birth,phone,integral,coupons,address,headImg} = req.body;
    let data = await usersService.update({_id:id,name,username,pwd,gender,birth,phone,integral,coupons,address,headImg});
    res.send(data);
});

router.post('/search' ,async function(req,res){
    console.log(req.body);
    // let {type,stu}=req.query
        let data=await usersService.search(req.body)
        res.send(data)
    
})

//图片上传
router.post("/upload", function (req, res) {
    var form = new multiparty.Form({ uploadDir: "./public/images" });
    form.parse(req, function (err, fields, files) {
      if (err) {
        res.send(err);
      } else {
        console.log(files.file);
        var p = files.file[0].path.substring(
          files.file[0].path.indexOf("images")
        );
        res.send({filename:path.basename(p)});
      }
    });
  });

module.exports = router;